<?php
/**
 * Created by PhpStorm.
 * User: yanggang
 * Date: 2018/4/17
 * Time: 下午2:43
 */

namespace app\admin\org;

//登录权限管理
use app\common\vendor\Des;

class Power
{
    //判断管理员是否登录
    public function checkHasLogin(&$out){
        $cname = 'appletadmin';
        $cookie = cookie($cname);
        $session = session($cname);
        $cookie = Des::decrypt($cookie);
        $cookie = json_decode($cookie,true);
        $client_ip = get_client_ip(0,true);
        if(!empty($session['account'])){
            if(empty($session['client_ip'])){
                return false;
            }
            if($client_ip == $session['client_ip'] && $session['expira']>time()){
                $out = array('account'=>$session['account'],'name'=>$session['real_name']);
                return true;
            }else{
                session($cname,null);
            }
        }elseif(!empty($cookie['account'])){
            if($client_ip == $cookie['client_ip'] && $cookie['expira']>time()){
                $out = array('account'=>$cookie['account'],'name'=>$cookie['real_name']);
                return true;
            }else{
                cookie($cname,null);
            }
        }else{
            $out = '';
            return false;
        }
    }

    public function loginok($account,$real_name,$power_id,$power_bicycle_place_id,$is_phone=false){
        $cookie['account'] = $account;
        $cookie['real_name'] = $real_name;
        $cookie['power_id'] = $power_id;
        $cookie['client_ip'] = get_client_ip(0,true);
        $cookie['expira'] = time()+86400*3;
        $cookie['power_bicycle_place_id'] = $power_bicycle_place_id;
        $cookies = json_encode($cookie);
        $cookies = Des::encrypt($cookies);
        $cname = 'appletadmin';
        if($is_phone)$cname = $cname.'_phone';
        cookie($cname,$cookies,86400*3);
        session($cname,$cookie);
        return true;
    }

    public function logout($is_phone=false){
        $cname = 'appletadmin';
        if($is_phone)$cname = $cname.'_phone';
        cookie($cname,null);
        session($cname,null);
        return true;
    }
}